package cn.laowen.org.service.impl;

import cn.laowen.basic.service.impl.BasicServiceImpl;
import cn.laowen.basic.util.PageList;
import cn.laowen.org.domain.Department;
import cn.laowen.org.mapper.DepartmentMapper;
import cn.laowen.org.query.DepartmentQuery;
import cn.laowen.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BasicServiceImpl<Department> implements IDepartmentService {

    //1.一般业务实现可以通公共业务实现层来实现

    //2.特殊的业务实现需要自己重写

    @Autowired
    private DepartmentMapper departmentMapper;



    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        //xml中开启自增id
        //新增部门之后 前端的dirPath 也要显示为： / +“上级部门id” 的格式
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        /*
         *修改之后
         *在前端页面dirPath列显示：找到上级部门 自动拼接成 “/” + “上级部门的id” 的格式显示出来
         *
         */
        //设置一个dirPath为空
        String dirPath = "";
        if (department.getParent()==null){
            //1.新增的是顶级部门 /+自己的id
            dirPath = "/"+department.getId();
        }else {
            //2.新增的不是顶级部门 找到自己的上级部门的 dirPath + / +自己的id
            Department parent = departmentMapper.loadById(department.getParent().getId());
            dirPath = parent.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void removeById(Long id) {
        departmentMapper.deleteById(id);
    }



    @Override
    public void batchRemove(List<Long> ids) {
        departmentMapper.batchRemove(ids);
    }

    /**
     * 无限级树
     * @return
     */
    @Override
    public List<Department> queryTree() {

        //1.一次性查询出所有数据，放到内存
        List<Department> departments = departmentMapper.loadAll();
        //为了后面找自己的父亲，这里把整体的部门数据，转换一份成Map<id,department>
        HashMap<Long, Department> allDepts = new HashMap<>();
        for (Department department : departments) {
            allDepts.put(department.getId(),department);
        }
        List<Department> firstDepts = new ArrayList<>();
        for (Department dept : departments) {
            // 2.从所有数据中找出第一层级 firstDepts   我们需要返回的最外数据
            if (dept.getParent_id() ==null){
                firstDepts.add(dept);
            }else {
                    // 3.只要不是第一层级的，找自己的上级，并将自己放入上级的children中
                Department parent = allDepts.get(dept.getParent_id());
                if (parent != null){
                    parent.getChildren().add(dept);
                }
            }
        }
        return firstDepts;
    }
}
